package com.saver.core.persistence;

import android.content.Context;

import com.saver.core.exception.DBException;

/**
 * Created by HGonzales.
 */
public class DBManager {

    private static final String ERROR_MESSAGE = "DataBase Manager no initialized";
    
    private Context context;
    private BancoSQLite bancoSQLite;
    private CategoriaEquivSQLite categoriaEquivSQLite;
    private CategoriaSQLite categoriaSQLite;
    private EstablecimientoEquivSQLite establecimientoEquivSQLite;
    private EstablecimientoSQLite establecimientoSQLite;
    private MonedaEquivSQLite monedaEquivSQLite;
    private MonedaSQLite monedaSQLite;
    private TransaccionSQLite transaccionSQLite;
    private TransaccionViewSQLite transaccionViewSQLite;
    private ParametroSQLite parametroSQLite;
    
    private DBManager() {
    }
    
    public static void initialize(Context context) {
        if (DBManagerSingleton.MANAGER.context == null) {
            DBManagerSingleton.MANAGER.context = context;
            DBManagerSingleton.MANAGER.createSQLite();
        }
    }
    
    private void createSQLite() {
        bancoSQLite = new BancoSQLite(context);
        categoriaSQLite = new CategoriaSQLite(context);
        categoriaEquivSQLite = new CategoriaEquivSQLite(context);
        establecimientoEquivSQLite = new EstablecimientoEquivSQLite(context);
        establecimientoSQLite = new EstablecimientoSQLite(context);
        monedaEquivSQLite = new MonedaEquivSQLite(context);
        monedaSQLite = new MonedaSQLite(context);
        transaccionSQLite = new TransaccionSQLite(context);
        transaccionViewSQLite = new TransaccionViewSQLite(context);
        parametroSQLite = new ParametroSQLite(context);
    }
    
    
    private static class DBManagerSingleton {
        public static final DBManager MANAGER = new DBManager();
        
    }
    
    public static BancoSQLite bancoSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.bancoSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static CategoriaEquivSQLite categoriaEquivSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.categoriaEquivSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static CategoriaSQLite categoriaSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.categoriaSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static EstablecimientoEquivSQLite establecimientoEquivSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.establecimientoEquivSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static EstablecimientoSQLite establecimientoSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.establecimientoSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static MonedaEquivSQLite monedaEquivSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.monedaEquivSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static MonedaSQLite monedaSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.monedaSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static TransaccionSQLite transaccionSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.transaccionSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static TransaccionViewSQLite transaccionViewSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.transaccionViewSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }

    public static ParametroSQLite parametroSQLite() {
        if (DBManagerSingleton.MANAGER.context != null) {
            return DBManagerSingleton.MANAGER.parametroSQLite;
        }
        throw new DBException(ERROR_MESSAGE);
    }
}
